class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sz = nums.size();
        int sum = 0;
        for (int i = 0; i < sz; i++) {
            sum += nums[i];
        }
        int target = sum - x;
        int left = 0, right = 0, ret = -1, cal = 0;
        if (target < 0)
            return -1;

        while (right < sz) 
        {
            cal += nums[right];
            while (cal > target) {
                cal -= nums[left++];
            }
            if (cal == target) {
                ret = max(ret, right - left + 1);
            }
            right++;
        }
        if (ret == -1)
            return ret;
        else
            return sz - ret;
    }
};
